
import {observable,action} from 'mobx'
import {CloudPermission, RoleList} from "../../../api/system/role/RoleApi";
import {HasPermissionList, PermissionList} from "../../../api/work/system/RuleApi";

class Store {
    // 搜索条件
    @observable searchMap = {};

    //列表
    @observable records = [];

    //详情
    @observable record = {};

    //表格加载
    @observable isFetching = false;

    //表单显示控制
    @observable formVisible = false;

    //弹窗加载控制
    @observable confirmLoading = false;

    //总数
    @observable total=0;

    //页码
    @observable page=1;

    //每页大小
    @observable pageSize=10;

    //后台权限分配弹窗控制
    @observable adminPermissionVisible = false;

    @observable adminConfirmLoading = false;

    @observable allPermissions = [];

    @observable allPermissionsChange = {};

    @observable allPermissionsMap = {};

    @observable allPermissionsReady = false;

    @observable permissions = {};

    @observable permissionsReady = false;

    //云图权限分配
    @observable cloudPermissionVisible = false;

    @observable allCloud = [];

    @observable hasCloud = [];

    @observable indeterminate = true;
    @observable checkAll = false;

    @action fetchRecords(){
        this.isFetching  = true;
        RoleList(this.searchMap).then(response => {
            if (response.data.code === 200){
                this.records = response.data.list
            }
        }).finally(() => this.isFetching = false)
    }

    @action showForm(record={}){
        this.record = record;
        this.formVisible = true;
    }

    @action handleReset = () => {
        this.searchMap = {};
        this.fetchRecords();
    };

    @action handleSearch = () => {
        this.fetchRecords();
    };

    @action initAllPermissions = () => {
        this.allPermissionsReady = false;
        PermissionList().then(response => {
            if (response.data.code === 200){

                this.allPermissions = response.data.list;
                this.allPermissions.forEach(item => {
                    this.allPermissionsChange[item["key"]] = item;
                    if (item.hasOwnProperty("pages")){
                        item.pages.forEach(page => {
                            let perms = [];
                            page.perms.forEach(value => {
                                perms.push(value.key)
                            });
                            if (!this.allPermissionsMap.hasOwnProperty(item.key)){
                                this.allPermissionsMap[item.key] = {};
                            }
                            if (!this.allPermissionsMap[item.key].hasOwnProperty(page.key)){
                                this.allPermissionsMap[item.key][page.key] = {"perms":[]};
                            }
                            this.allPermissionsMap[item.key][page.key]["perms"] = perms;
                            this.allPermissionsMap[item.key][page.key]["sub_type"] = page.sub_type;
                        })
                    }
                })
            }
        }).finally(() => this.allPermissionsReady = true);
    };

    @action initPermissions = () => {
        this.permissionsReady = false;
        HasPermissionList({id:this.record.id}).then(response => {
            if (response.data.code === 200){
                this.permissions = response.data.list;
            }
        }).finally(() => this.permissionsReady = true)
    };

    @action initCloudPermission = () => {
        CloudPermission({id:this.record.id}).then(response => {
            if (response.data.code === 200){
                this.allCloud = response.data.allCloud;
                this.hasCloud = response.data.hasCloud;
            }
        })
    };
}

export default new Store();
